package com.qinglian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinglian.entity.Friend;
import com.qinglian.entity.User;
import com.qinglian.mapper.FriendMapper;
import com.qinglian.mapper.UserMapper;
import com.qinglian.service.IFriendService;
import com.qinglian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author augn
 * @since 2023-04-24
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements IFriendService {

    @Autowired
    private FriendMapper mapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public Result<?> getFriendList(Integer userId) {
        List<Integer> friendIdList = mapper.getFriendId(userId);
        List<Map<String,Object>> friendList = new ArrayList<>();

        for (Integer friendId : friendIdList) {
            User user = userMapper.selectById(friendId);
            Map<String,Object> friend = new HashMap<>();
            friend.put("id",user.getId());
            friend.put("image",user.getImage());
            friend.put("account",user.getAccount());
            friend.put("name",user.getName());
            friend.put("personalSignature",user.getPersonalSignature());
            friendList.add(friend);
        }

        return Result.success(friendList);
    }

    @Override
    public Result<?> getFriendImageList(Integer userId) {
        List<Integer> friendIdList = mapper.getFriendId(userId);
        Map<String,Object> friendImage = new HashMap<>();
        for (Integer friendId : friendIdList) {
            User user = userMapper.selectById(friendId);
            friendImage.put(user.getId()+"",user.getImage());
        }

        return Result.success(friendImage);
    }

    @Override
    public Result<?> getFriendNameList(Integer userId) {
        List<Integer> friendIdList = mapper.getFriendId(userId);
        Map<String,Object> friendName = new HashMap<>();
        for (Integer friendId : friendIdList) {
            User user = userMapper.selectById(friendId);
            friendName.put(user.getId()+"",user.getName());
        }

        return Result.success(friendName);
    }

    @Override
    public Result<?> getFriendNoteList(Integer userId) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.and(i -> i.eq("user_id", userId).eq("agree", 1));
        List<Friend> friends = mapper.selectList(wrapper);
        Map<String,Object> friendNote = new HashMap<>();
        for (Friend friend : friends) {
            friendNote.put(friend.getFriendId()+"",friend.getNote());
        }
        return Result.success(friendNote);
    }

    @Override
    public Result<?> addFriend(Friend friend) {
        int insert = mapper.insert(friend);
        if (insert == 0) {
            return Result.error("-1","添加错误");
        } else {
            return Result.success();
        }
    }

    @Override
    public Result<?> getReqFriendList(Integer userId) {
        List<Integer> friendIdList = mapper.getReqFriendId(userId);
        List<Map<String,Object>> friendList = new ArrayList<>();

        for (Integer friendId : friendIdList) {
            User user = userMapper.selectById(friendId);
            Map<String,Object> friend = new HashMap<>();
            friend.put("id",user.getId());
            friend.put("image",user.getImage());
            friend.put("account",user.getAccount());
            friend.put("name",user.getName());

            QueryWrapper<Friend> wrapper = new QueryWrapper<>();
            wrapper.and(i -> i.eq("user_id", user.getId()).eq("friend_id", userId).eq("agree", 0));
            Friend friend1 = mapper.selectOne(wrapper);

            friend.put("content",friend1.getContent());
            friendList.add(friend);
        }

        return Result.success(friendList);
    }

    @Override
    public Result<?> agreeOrNot(Integer userId, Integer friendId, Integer type) {

        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.and(i -> i.eq("user_id", friendId).eq("friend_id", userId)).orderByDesc("create_time").last("limit 1");
        Friend friend = mapper.selectOne(wrapper);
        friend.setAgree(type);
        int update = mapper.updateById(friend);
        if (type == 1) {
            Friend friend1 = new Friend();
            friend1.setUserId(userId);
            friend1.setFriendId(friendId);
            friend1.setAgree(1);
            User user = userMapper.selectById(friendId);
            friend1.setNote(user.getName());
            int insert = mapper.insert(friend1);
        }

        if (update == 1) {
            return Result.success();
        } else {
            return Result.error("-1","处理失败");
        }

    }


}
